'''
Created on June 20, 2011

@author: yc30rr1
'''
import os
import datetime
import string
import time, os, fnmatch, shutil
#This decorator is used for all singleton Classes
def singleton(cls):
        instances = {}
        def getinstance():
            if cls not in instances:
                instances[cls] = cls()
            return instances[cls]
        return getinstance

class Utils():
    
    '''  
    Returns Base Path of the Application
    The base path have to be appended with a forward of backward slash
    '''
    def baseAppPath(self):
        path = os.path.dirname(__file__)
        path = os.path.dirname(path)
        #path = os.path.dirname(path)
        #path = os.path.dirname(path)
        return path + self.seperator()
    
    def seperator(self):
        return os.sep
        
    def getTimeStamp(self):
        now = datetime.datetime.now()
        date_time=now.strftime("%Y-%m-%d %H:%M:%S")
        datetime1=str(date_time).split(' ')
        Curr_Date=datetime1[0]
        timeStamp= datetime1[1]
        return Curr_Date,timeStamp
    
    def getDateStamp(self):
        datetime = str(self.getTime()).split(' ')
        return datetime[0]

    def getTime(self, format="%Y-%m-%d %H:%M:%S"):
        now = datetime.datetime.now()
        return now.strftime(format)
    
    #def listToHtml(self,listValues):
    #    htmlcode =Table(listValues)
    #    return  htmlcode
    
    def createDir(self,dirname):
        cwd=os.getcwd()
        dircreatPath=cwd[0:string.index(cwd,'src')]
        if not os.path.isdir(os.path.join(dircreatPath, dirname)):
            dirname= os.makedirs(os.path.join(dircreatPath, dirname))
        
    def createArchive(self):
        '''
        This method used to Copy files to Folder_Backup\timestamp directory for every script run
        '''
        t = time.localtime()
        timestamp = time.strftime('%b-%d-%Y_%H%M', t)
        bu_dir = os.path.join(r'.\Folder_Backup', timestamp)
        proj_dir = os.getcwd()
        mkdir_cmd = r'mkdir ' + bu_dir
        #execute mkdir Folder_Backup\timestamp
        os.system(mkdir_cmd)
        #Copy files to Folder_Backup\timestamp directory on Buffy
        for d in os.listdir(proj_dir):
            if not fnmatch.fnmatch(d, '*Folder_Backup*'): #don't backup Folder_Backup
                #backup directories and subdirectories
                if os.path.isdir(d):
#                    print 'backing up ' + d
                    shutil.copytree(d, os.path.join(bu_dir, d))
                    #backup loose files in the project directory
                else:
#                    print 'backing up ' + d
                    shutil.copy(d, os.path.join(bu_dir, d))
      
    
    

            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
        
        
        